package com.yuanluludev.app.room

import androidx.lifecycle.viewModelScope
import com.yuanluludev.app.R
import com.yuanluludev.app.room.book.Book
import com.yuanluludev.app.room.book.Family
import com.yuanluludev.app.room.student.Student
import com.yuanluludev.app.room.student.StudentHelper
import com.yuanluludev.lib_base.base.BaseVmActivity
import com.yuanluludev.lib_utils.util.GsonUtils
import com.yuanluludev.lib_utils.util.LogUtils
import kotlinx.android.synthetic.main.activity_room.*
import kotlinx.coroutines.launch

class RoomActivity : BaseVmActivity<RoomViewModel>() {

    override fun initEvents() {

        //插入一个学生
        tvInsertStudent?.setOnClickListener {
            val stu = Student(
                name = "袁老师",
                age = 18,
                address = "重庆市高新区",
//                family = Family(father = "张骞", mother = "徐晃"),
                books = ArrayList<Book>().apply {
                    add(Book(name = "周文王的老师", price = 11.2))
                    add(Book(name = "多年未见的兄弟", price = 44.2))
                    add(Book(name = "蓝本而", price = 32.2))
                },
                family = Family().apply {
                    father = "秦始皇"
                    mother = "虞姬"
                }
            )
            mViewModel.insertStudent(stu)
        }

        //查询所有学生
        tvQueryStudent?.setOnClickListener {
            mViewModel.getAllStudent()
        }

        //查询所有学生(根据uid降序查询)
        tvQueryAllStudentOrderByUidDesc?.setOnClickListener {
            mViewModel.queryAllStudentOrderByUidDesc()
        }

        //操作
        tvOperation?.setOnClickListener {

            mViewModel.viewModelScope.launch {
                //根据uid查询
//                val bean = StudentHelper.queryStudentByUid(2)
//                bean?.apply {
//                    name = "李隆基"
//                    StudentHelper.updateStudent(this)
//                }

                //模糊查询
//                val l = StudentHelper.queryStudentLikeName("%李%")

                //插入一部分学生
//                val l = ArrayList<Student>().apply {
//                    add(Student(name = "诸葛亮", age = 18, address = "蜀国战区"))
//                    add(Student(name = "曹操", age = 28, address = "魏国战区"))
//                    add(Student(name = "孙权", age = 38, address = "吴国战区"))
//                }
//                StudentHelper.insertStudents(*l.toTypedArray())


                //插入一个学生带家庭
                StudentHelper.insertStudent(
                    Student(
                        name = "周瑜",
                        age = 33,
                        address = "吴国大巴",
//                        family = Family(father = "张三", mother = "里斯"),
//                        books = ArrayList<Book>().apply {
//                            add(Book(name = "动物世界", price = 11.2))
//                            add(Book(name = "河北的姑娘", price = 44.2))
//                            add(Book(name = "南京大屠杀", price = 32.2))
//                        }
                    )
                )

//                LogUtils.json(GsonUtils.toJson(l))

            }

        }

        //根据ID查询
        tvDelete?.setOnClickListener {
            mViewModel.deleteStudentByID(3)
        }

    }

    override fun observe() {
        mViewModel.run {
            //查询到的所有学生
            studentList.observe(this@RoomActivity) {
                LogUtils.json(GsonUtils.toJson(it))
            }
        }
    }

    override fun getLayoutResId(): Int {
        return R.layout.activity_room
    }

    override fun viewModelClass(): Class<RoomViewModel> = RoomViewModel::class.java

}